Skip to content

Add erasable phantom classes (old) #1408

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

nicolasstucki
Copy link
Contributor

@nicolasstucki nicolasstucki commented Jul 21, 2016

@xeno-by
Copy link

xeno-by commented Jul 21, 2016

Sounds interesting, but lacks any kind of description. What are these phantom types? Why would one want to use them?

@nicolasstucki
Copy link
Contributor Author

For now the PR is just for testing purposes. I will add a full description once it is more mature.

The basic idea is to have parameters/field that will get erased (term wise). This will be needed for efficient implementation of implicit evidences for the capability based effect system.

@odersky
Copy link
Contributor

odersky commented Jul 21, 2016

In that case we should mark the title of the PR with [WIP]. I think the "in progress" label is not specific enough.

@nicolasstucki nicolasstucki changed the title Add erasable phantom types. [WIP] Add erasable phantom types. Jul 21, 2016
@nicolasstucki nicolasstucki force-pushed the implement-phantom-types branch 15 times, most recently from 7fc880b to 36a4048 Compare July 27, 2016 12:37
@nicolasstucki
Copy link
Contributor Author

/rebuild

@nicolasstucki nicolasstucki force-pushed the implement-phantom-types branch 4 times, most recently from f463cb0 to d0793e7 Compare July 29, 2016 13:08
@nicolasstucki nicolasstucki force-pushed the implement-phantom-types branch 5 times, most recently from 3be5349 to 0ed4e3c Compare August 9, 2016 14:56
@nicolasstucki nicolasstucki force-pushed the implement-phantom-types branch 11 times, most recently from 487e143 to 9c83c67 Compare March 8, 2017 15:16
@nicolasstucki nicolasstucki force-pushed the implement-phantom-types branch 2 times, most recently from ae0560d to 7429b27 Compare March 16, 2017 14:37
@nicolasstucki nicolasstucki force-pushed the implement-phantom-types branch from 7429b27 to d51ea40 Compare March 20, 2017 08:53
@nicolasstucki nicolasstucki force-pushed the implement-phantom-types branch from d51ea40 to 74bb24e Compare March 22, 2017 10:38
In this commit phantom types are defined and implemented.
Only a minimal version of phantom term erasure is included.

Phantom types are in latices outside of the normal `Any`/`Nothing` type lattice.
A new phantom lattice can be defined by extending an `object` with `scala.Phantom`.
This trait defines synthetic members
* `protected final trait Any` where this `Any` does not extends `scala.Any`
* `protected final abstract class Nothing extends this.Any`
* `protected final def assume[P >: this.Nothing <: this.Any]: P`

A phantom lattice object can expose any of the phantom members (`Any`, `Nothing`, `assume`)
using an alias but it is not required.

Restriction on lattices:
* Types from different lattice can not be mixed using `&` or `|` types.
* Type parameters must be bounded by types in a single lattice.

Phantom type erasure (minimalistic):
* Type references `Phantom.Any` and `Phantom.Nothing` are erased to `dotty.runtime.ErasedPhantom`
* `Phantom.assume[P]` is erased to `null.asInstanceOf[P]`
* `Phantom` is erased to `dotty.runtime.ErasedPhantomLattice`, removing the members of `Phantom`
This commit removes arguments and parameter of phantom type from all applications.
Arguments are evaluated before the application in case they have side effects.

```
def foo(i: Int, p: SomePhantom) = ???

foo(42, { println("hello"); getSomePhantom })
```

becomes

```
def foo(i: Int) = ???

val x$0 = 42
val x$1 = { println("hello"); getSomePhantom }
foo(x$0)
```

Note that now `def foo(i: Int)` and def `foo(i: Int, p: SomePhantom)` erase to the same signature.

Tests for this commit where added in `Extra tests for Phantoms 1`, they where back ported as the semantics and runtime is not affected.
Erase all `val` and `var` of phantom type. `lazy val` are not transformed as their expansion is done in the backend.

Erasure:
* Assignments are replaced by their RHS
* Local `val` and `var` definitions are replaced by their RHS
* Fields are removed
* Getters return `null.asInstanceOf[dotty.runtime.ErasedPhantom]`

Tests for this commit where added in `Extra tests for Phantoms 2`, they where back ported as the semantics and runtime is not affected.
@nicolasstucki nicolasstucki force-pushed the implement-phantom-types branch from 74bb24e to 946a331 Compare March 22, 2017 12:56
@nicolasstucki nicolasstucki changed the title Add erasable phantom classes Add erasable phantom classes (old) Apr 21, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.